const urlParams = new URLSearchParams(new URL(window.location.href).search);
let ids = urlParams.get('id');
let rows = {}

$().ready(function() {
	imageCreate()
	validateRule();
	inputChange()
	dragoverImg()
	closeChange()
	domCreate()
	uploadCreate()
});

$.validator.setDefaults({
	submitHandler: function () {
		onSubmit();
	}
});





//确认
async function onSubmit() {
	let data = rows
	data.name = $('#name').val()
	data.sort = $('#sort').val()
	let res = await valueCreate()
	data.chapterDocuments = [...res]

	save(data)
}

// 数据处理
function valueCreate() {
	let list =[]
	let filValue = [
		{
			parent:'#imgAll',
			children: '.imgValue',
			type: '4'
		},
		{
			parent:'#videos',
			children: '.textOverflow',
			type: '3'
		},
		{
			parent:'#document',
			children: '.textOverflow',
			type: '2'
		},
		{
			parent:'#pagesall',
			children: '.textOverflow',
			type: '1'
		},
		{
			parent:'#audio',
			children: '.textOverflow',
			type: '5'
		}
	]

	for (let i = 0; i < filValue.length; i++) {
		// 获取所有的上传的内容，处理数据
		let value = $(`${filValue[i].parent} ${filValue[i].children}`).map(function() {
			return $(this).attr('data-name')
		}).get();
		for (let j = 0; j < value.length; j++) {
			let images = {
				chapterId:rows.id,
				coursesId:rows.coursesId,
				file:'',//文件
				type:''
			}
			images.type = filValue[i].type
			images.file = value[j]
			list.push(images)
		}
	}
	return list
}


// 图片处理
function imageCreate() {
	$('#imgAll .imgValue').click(function (event){
		event.preventDefault()
		var imageUrl = $(this).attr('src');
		$('.overlays').addClass('active');
		$('.overlays img').attr('src', imageUrl);
	})
	$('.overlays').click(function() {
		$(this).removeClass('active');
	});

}



//编辑
function save(data) {
	$.ajax({
		type : "POST",
		url : "/chapter/updateCharpter",
		contentType: 'application/json',
		data : JSON.stringify(data),
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		},
		error: function(xhr, status, error) {
			// 请求失败时的处理
			layer.msg(error);
		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#chapterResource").validate({
		rules : {
			name:{
				required : true
			},
		}
	})
}


// 输入框事件
function inputChange() {
	$('#sort').blur(function() {
		let value = $(this).val();
		let data = {
			sort: ''
		}
		data.sort = value
		if(value !== ''){
			inputFrom(data)
		}

	});
}

//优先级接口
function inputFrom(data) {
	return new Promise(function (resolve, reject) {
		$.ajax({
			url: '/chapter/effect',
			type: 'POST',
			contentType: 'application/json',
			data: JSON.stringify(data),
			success: function(response) {
				// 请求成功时的处理
				if(response.code !== '0') {
					layer.msg(response.msg);
					$('#sort').val('')
					return
				}
			},
			error: function(xhr, status, error) {
				// 请求失败时的处理
				layer.msg(error);
				resolve(error)
			}
		});
	})
}

//数据回填
async function domCreate() {
	let {data} = await addFrom(ids)
	let datas = data
	$('#name').val(datas?.name)
	$('#sort').val(datas?.sort)
	let list = datas?.chapterDocuments
	for (let i = 0; i < list.length; i++) {
		await typeDom(list[i]?.file,list[i].type * 1,list[i].id)
	}
}


// 详情
function addFrom(id) {
	return new Promise(function (resolve, reject) {
		$.ajax({
			url: `/chapter/selectById?id=${id}`,
			type: 'get',
			success: function(data) {
				// 请求成功时的处理
				if(data.code !== '0') {
					layer.msg(data.msg);
				}else {
					rows = data.data
					resolve(data)
				}

			},
			error: function(xhr, status, error) {
				// 请求失败时的处理
				layer.msg(error);
				resolve(error)
			}
		});
	})

}

